package com.stm.web.controller.equipmentMonitor;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stm.common.annotation.Log;
import com.stm.common.core.controller.BaseController;
import com.stm.common.core.domain.AjaxResult;
import com.stm.common.core.page.TableDataInfo;
import com.stm.common.enums.BusinessType;
import com.stm.common.utils.poi.ExcelUtil;
import com.stm.equipmentMonitor.domain.Equipment;
import com.stm.equipmentMonitor.domain.Equipmenthis;
import com.stm.equipmentMonitor.service.IEquipmentService;
import com.stm.equipmentMonitor.service.IEquipmenthisService;
import com.stm.framework.util.ShiroUtils;

/**
 * 设备配置 信息操作处理
 * 
 * @author wyc
 * @date 2019-04-23
 */
@Controller
@RequestMapping("/equipmentMonitor/equipment")
public class EquipmentController extends BaseController
{
    private String prefix = "equipmentMonitor/equipment";

    @Autowired
	private IEquipmenthisService equipmenthisService;
	
	@Autowired
	private IEquipmentService equipmentService;
	
	@RequiresPermissions("equipmentMonitor:equipment:view")
	@GetMapping()
	public String equipment(ModelMap mmap)
    {
    	mmap.put("user", ShiroUtils.getSysUser());
	    return prefix + "/equipment";
	}
	
	/**
	 * 查询设备配置列表
	 */
	@RequiresPermissions("equipmentMonitor:equipment:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Equipment equipment)
	{
		startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
		return getDataTable(list);
	}

	/**
	 * 打开报表页面
	 */
	@RequiresPermissions("equipmentMonitor:equipment:chart")
	@GetMapping("/historyChart/{id}")
	public String detail(@PathVariable("id") String id, ModelMap mmap) {
		//根据id查出历史数据
		//Equipmenthis historyData = equipmenthisService.selectEquipmenthisById(id);
		Equipment equipment = equipmentService.selectEquipmentById(id);
		//put值
		mmap.put("equipment",equipment);
		mmap.put("thisDay",new Date());
		return prefix + "/historyChart";
	}

	/**
	 * 获取报表数据
	 */
	@ResponseBody
	@PostMapping("/getChartData")
	public List<Map<String, Object>> getChartData(Equipmenthis equipmenthis){
		return equipmenthisService.getChartData(equipmenthis);
	}

	/**
	 * 导出设备配置列表
	 */
	@RequiresPermissions("equipmentMonitor:equipment:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Equipment equipment)
    {
    	List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        return util.exportExcel(list, "equipment");
    }
	
	/**
	 * 新增设备配置
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存设备配置
	 */
	@RequiresPermissions("equipmentMonitor:equipment:add")
	@Log(title = "设备配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Equipment equipment)
	{
		equipment.setId(UUID.randomUUID().toString());
		equipment.setDatelong(0);
		return toAjax(equipmentService.insertEquipment(equipment));
	}

	/**
	 * 修改设备配置
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		Equipment equipment = equipmentService.selectEquipmentById(id);
		mmap.put("equipment", equipment);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存设备配置
	 */
	@RequiresPermissions("equipmentMonitor:equipment:edit")
	@Log(title = "设备配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Equipment equipment)
	{		
		return toAjax(equipmentService.updateEquipment(equipment));
	}
	
	/**
	 * 删除设备配置
	 */
	@RequiresPermissions("equipmentMonitor:equipment:remove")
	@Log(title = "设备配置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(equipmentService.deleteEquipmentByIds(ids));
	}
	
}
